import APPOPTIONS from "@/app.config";
import { mobxState } from "@/data";
import { toJS } from "mobx";


/**
 * @description 用来初始化项目的时候加载激活的tabKey
 */
const loadTabs = () => {

    if (!APPOPTIONS.FRAME_TAB) return;
    const { pathname = String } = window.location;
    let params = pathname.split("/");
    //1、未登录，不用加载tabs标签
    if (!sessionStorage.token) return;
    console.log("已经登录了，加载tabs标签")
    let lastParams = params[params.length - 1];

    /**
     * 已经登录：
     * 是不是404不存在的页面？如果是，报404，如果不是，再判断有没有缓存。
     * 有没有缓存 ？ 
     *   有缓存：使用缓存tabs和active
     *   没缓存：打开第1个路由
     */




    if (lastParams === "admin" || lastParams === "login") {
        console.log("最后一个path参数", lastParams)
        const tabs = toJS(mobxState.routes);
        const firstRoute = tabs[0].children[0];
        // const { key, menuName, path, state = {} } = firstRoute;
        const { id, menuName, routerPath, state = {}
        } = firstRoute;
        if (id && menuName) {
            mobxState.addTabs({
                key: id,
                label: menuName,
                path: routerPath,
                state
            })
            return;
        }
        return;


    }


    const activeTabKey = params[params.length - 1]

    // 判断里面有没有这个tabs
    const tabs = toJS(mobxState.tabs);
    const tab = tabs.filter(item => item.id === params[params.length - 1]);
    if (tab.length === 0) {
        // 如果没有初始可用的标签，则加载路由中第1个可用标签
        const routes = mobxState.routes;
        let object = {};

        const filterList = (list, prentStr) => {

            for (var i = 0; i < list.length; i++) {
                let route = list[i];

                if (route.id === activeTabKey) {
                    // debugger 
                    object = {
                        ...route,
                        // path: `${prentStr}/${item.path}`
                        path: route.routerPath
                    }
                    break;
                } else {
                    if (route.children && route.children.length > 0) {
                        filterList(route.children, route.routerPath)
                    }
                }
            }
        }

        filterList(toJS(routes), "/");
        const { menuName, id, routerPath, state = {} } = object
        if (!routerPath) return;
        // 即不是下面的路径 也不在路由里面，就404
        if (routerPath !== "/" && routerPath !== "admin" && routerPath !== "/admin" && routerPath !== "login" && routerPath !== "/login") {
            mobxState.addTabs({
                label: menuName,
                key: id,
                path: routerPath,
                state
            })
        }

    } else {

        // mobxState.setActiveTab(activeTabKey);
    }

    // }

}


/**@description 跳到页面为404的时候返回时tab的处理 */
const refreshError = (callBack) => {
    const tabs = toJS(mobxState.tabs);
    const newTabs = tabs.filter(item => typeof (item.label) === "string" && typeof (item.path) === "string");
    const length = newTabs.length;
    const lastIndex = length - 2;
    const object = newTabs[lastIndex];
    // label, path, key, state
    let resArray = newTabs.map((item, index) => {
        console.log(item, index)
        if (index <= lastIndex) {
            return item;
        }
    }).filter(Boolean)
    mobxState.setTabs(resArray);
    // 清除最后一项
    mobxState.onChangeTab(object)
    callBack && callBack(object.path)
}


export default loadTabs;
export {
    refreshError
}